package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AsecuAuditBaseService;
import cn.bdqn.pojo.Know;
import cn.bdqn.pojo.User;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审计知识库管理
 * @author 8
 */
@Controller
public class AuditBaseController {

    @Autowired
    AsecuAuditBaseService asecuAuditBaseService;


    @RequestMapping("/shenjizhishiku-list")
    public String shenjizhishikuList(Model model){
        List<Know> knows = asecuAuditBaseService.getData();
        model.addAttribute("knows",knows);
        return "shenjizhishiku-list";
    };

    @RequestMapping("/fujian-add")
    public String fujianAdd(){
        return "fujian-add";
    };

    @PostMapping("/fileUpload")
    public @ResponseBody Map upload(@RequestParam("file") MultipartFile file, String[] keypoints,HttpServletRequest request){
        StringBuffer kp= new StringBuffer();
        for (String s : keypoints) {
            kp.append(s+"、");
        }
        kp.deleteCharAt(kp.length()-1);
        String s = kp.toString();
        User user = (User) request.getSession().getAttribute("user");

        this.asecuAuditBaseService.storeFile(file,s,user.getName());
        Map map = new HashMap();
        map.put("status",200);
        return map;
    }


    @PostMapping({ "/removeFile" })
    @ResponseBody
    public String removeFile(Integer id) {
        this.asecuAuditBaseService.removeFile(id);
        return "删除成功";
    }
    @GetMapping({ "/downloadFile" })
    @ResponseBody
    public ResponseEntity<Resource> downloadFile(Integer id, HttpServletRequest request) {
        Know know = this.asecuAuditBaseService.getFileInfoById(id.longValue());
        Resource resource = this.asecuAuditBaseService.loadFileAsResource(id.longValue());

        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        try {
            return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
                    .header("Content-Disposition",
                            "attachment; filename=\""
                                    + new String(know.getRealFileName().getBytes("GB2312"), "ISO8859-1") + "\"")
                    .body(resource);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }


}
